package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.common.annotations.Log;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.pojo.system.SysUser;
import com.atguigu.spzx.model.request.system.QueryRequest;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.utils.DYLAssert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dyl
 * @version 1.0
 * @description:
 * @date 2024/12/27 23:02
 */
@RestController
@RequestMapping("/user/system/sysUser")
@Tag(name = "用户接口")
@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class SysUerController {
    @Autowired
    private SysUserService sysUserService;

    //    @Log(title = "用户模块", businessType = 0)
    @GetMapping("/findUserByPage/{pageNum}/{pageSize}")
    @Operation(summary = "分页查询")
    public Result<Page<SysUser>> findUserByPage(@PathVariable("pageNum") @Parameter(name = "页面编号", required = true)
                                                Integer currentPage,
                                                @PathVariable("pageSize") @Parameter(name = "页面大小", required = true) Integer pageSize,
                                                @Parameter(name = "查询条件", required = false) QueryRequest queryRequest) {
        DYLAssert.notNull(currentPage, "分页错误");
        DYLAssert.notNull(pageSize, "分页错误");
        DYLAssert.notNull(queryRequest, "分页错误");
        //Page<SysUser> page = sysUserService.findUserByPage(pageNum, pageSize, queryRequest);
        return Result.ok(sysUserService.findUserByPage(currentPage, pageSize, queryRequest));
    }

    @PostMapping("/addUser")
    @Operation(summary = "添加用户")
    public Result addUser(@RequestBody SysUser sysUser) {
        DYLAssert.isTrue(StringUtils.isAnyEmpty(sysUser.getName(), sysUser.getPhone()), "添加失败");
        DYLAssert.isTrue(sysUserService.save(sysUser), "添加失败");
        return Result.ok();
    }

    @PutMapping("/updateUser")
    @Operation(summary = "修改用户")
    public Result updateUser(@RequestBody SysUser sysUser) {
        DYLAssert.notNull(sysUser.getId(), "修改失败，id为空");
        DYLAssert.isTrue(StringUtils.isAnyEmpty(sysUser.getName(), sysUser.getPhone()), "修改失败");
        DYLAssert.isTrue(sysUserService.updateById(sysUser), "修改失败");
        return Result.ok();
    }

    @GetMapping("/findUserById/{id}")
    @Operation(summary = "根据Id查询用户")
    public Result<SysUser> selectUserById(@PathVariable("id") Long id) {
        DYLAssert.notNull(id, "查询失败，id为空");
        return Result.ok(sysUserService.getById(id));
    }

    @DeleteMapping("/deleteUserById/{id}")
    @Operation(summary = "根据Id删除用户")
    public Result deleteUserById(@PathVariable("id") Long id) {
        DYLAssert.notNull(id, "删除失败，id为空");
        DYLAssert.isTrue(sysUserService.removeById(id), "删除失败");
        return Result.ok();
    }

    @DeleteMapping("/batchDelete")
    @Operation(summary = "批量删除")
    public Result batchDelete(@RequestBody List<Long> ids) {
        DYLAssert.notNull(ids, "删除失败，id为空");
        DYLAssert.isTrue(sysUserService.removeByIds(ids), "删除失败");
        return Result.ok();
    }
}
